'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('users', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
        allowNull: false,
        comment: '用户唯一标识',
      },
      username: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '用户名',
      },
      password_hash: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '密码哈希值',
      },
      grade_level: {
        type: Sequelize.ENUM('primary', 'middle', 'high'),
        allowNull: false,
        comment: '年级层次：primary-小学，middle-初中，high-高中',
      },
      last_login_at: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '最后登录时间',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
        comment: '创建时间',
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
        comment: '更新时间',
      },
    }, {
      engine: 'InnoDB',
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
      comment: '用户表',
    });

    // 创建索引
    await queryInterface.addIndex('users', ['username'], {
      name: 'idx_username',
      unique: true,
    });

    await queryInterface.addIndex('users', ['grade_level'], {
      name: 'idx_grade_level',
    });

    await queryInterface.addIndex('users', ['created_at'], {
      name: 'idx_created_at',
    });

    console.log('✅ Users table created successfully');
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('users');
    console.log('✅ Users table dropped successfully');
  },
};

